<?php

class  spider{

	public static function spider_types(){
		return array(
			'Baiduspider' => '百度',
			'Googlebot' => '谷歌',
			'Sogou' => '搜狗',
			'Bingbot' => '必应',
			'Yisouspider' => '神马', 
			'HaoSouSpider' => '360搜索',
			//'Safari' => '浏览器'
			);
	}

	public static  function write($yuming_id){

		$today = date("Ymd");

		$spider_types = spider::spider_types();
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
		$test = false;
		$spidertype = '';
		foreach ($spider_types as $k => $v){
			$test = stripos($user_agent, $k)!==false;
			if($k == 'HaoSouSpider'){
				$test = $test || stripos($user_agent, '360Spider')!==false;
			}

			if ($test) {
				$spidertype = $k;
			}
		}

		$spider_dir = DATA.DS.'spider';
		if(!is_dir($spider_dir)) mkdir($spider_dir, 0777, true);
		
		$site_dir = $spider_dir.DS.$yuming_id;
		if(!is_dir($site_dir)) mkdir($site_dir, 0777, true);

		//访问量
		if ($test == false) {
			$url = trim($_SERVER['REQUEST_URI'],'/');
			$first_dir = substr($url, 0, strpos($url, '/'));
			$ext = substr($url, strpos($url, '.')+1);

			$difdir = array('templets','uploads','data','static');
			$difext = array('jpg','png','gif','css', 'js', 'swf');
			if (in_array($first_dir,$difdir) || in_array($ext, $difext)) {
				return;
			}
			$time = time();
			$lasttime = db::getField("select varValue from config where varName='visitTime'");
			$lastday = date("Ymd",$lasttime);

			if ($lastday==$today) {
				db::query("update config set varValue=varValue+1 where varName='visitCount'");
			} else {
				db::query("update config set varValue=1 where varName='visitCount'");
			}
			db::query("update config set varValue='$time' where varName='visitTime'");

		} else {	//爬虫
			if (empty($GLOBALS['G']['spider']) || $GLOBALS['G']['spider']!=1) {
				return;
			}
			$url = get_url();
			$log_file = $site_dir.DS.date("YmdH").".log";
			$fp = fopen($log_file,"a");
			if($fp){
				$timestamp = time();
				$content = "{$spidertype},{$timestamp},{$url}";
				$flag = fwrite($fp,$content."\r\n");
				fclose($fp);
			}
			$total_file = $site_dir.DS."spider.php";

			if(is_file($total_file)) {
				$spider = require $total_file;
				if(!is_array($spider)) {
					$spider = array();
				}
			} else {
				$spider = array();
			}
			
			if (count($spider)>30) {
				$spider = array_slice($spider,1);
			}
			
			if (isset($spider[$today][$spidertype])) {
				$spider[$today][$spidertype] = $spider[$today][$spidertype]+1;
			} else {
				$spider[$today][$spidertype] = 1;
			}

			if (gp('intdar')) {
				echo $total_file;
				dump($spider);
				dump($GLOBALS['G']);
			}
			
			$spider = '<?php return '.var_export($spider, true).';';
			file_put_contents($total_file, $spider,LOCK_EX);
		}

		$rand = rand(0,100);
		if($rand==0){
			$read_dir = @scandir($site_dir);
			foreach ($read_dir as $v) {
				if(strpos($site_dir.DS.$v, '.php')==false && $v!='.' && $v!='..' && filemtime($site_dir.DS.$v) < strtotime('-1 days')){
					unlink($site_dir.DS.$v);
				}
			}
		}

	}
}
